package com.zf.jsider;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zf.jsider.entity.Character;
import com.zf.jsider.service.ExpToExcelService;

/**
 * @author zhangfeng
 *
 */
public class App {
	public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
		int page = 1;
		System.out.println("TLBB爬虫");
		System.out.println("url:");
		Scanner scanner = new Scanner(System.in);
		String url = scanner.nextLine();
		Document document = null;
		Elements elements = new Elements();
		List<Character> characters = new ArrayList<>();
		do {
			try {
				document = Jsoup.connect(url).userAgent("Mozilla").data("page_num", "" + page++).get();
			} catch (IOException e) {
				System.out.println("url输入不正确！");
			}
			if (document != null) {
				Elements temp = document.getElementsByClass("role-item");
				if (temp != null)
					elements.addAll(temp);
			} else
				break;
		} while (document.getElementsByClass("after lock1").isEmpty());
		for (int i = 0; i < elements.size(); i++) {
			Element element = elements.get(i);
			Character temp = new Character();
			Element ele = element.getElementsByClass("title").get(0);
			temp.setInfo(ele.text());
			temp.setUrl(ele.select("a").attr("href"));
			ele = element.getElementsByClass("detail").get(0);
			temp.setSequipment(ele.text());
			ele = element.getElementsByClass("price").get(0);
			temp.setPrice(Integer.valueOf(ele.text().substring(1)));
			System.out.println(i);
			if (temp.getUrl() != null) {
				try {
					document = Jsoup.connect(temp.getUrl()).userAgent("Mozilla").get();
				} catch (Exception e) {
					Thread.sleep(3000);
					i--;
					continue;
				}
				element = document.getElementById("goods-detail");
				ele = element.getElementById("bing");
				temp.setSice(ele.text());
				String ss = ele.select("p:contains(冰攻击)").text().substring(5);
				temp.setIce(Integer.valueOf(ss));

				ele = element.getElementById("huo");
				temp.setSfire(ele.text());
				ss = ele.select("p:contains(火攻击)").text().substring(5);
				temp.setFire(Integer.valueOf(ss));

				ele = element.getElementById("xuan");
				temp.setSthunder(ele.text());
				ss = ele.select("p:contains(玄攻击)").text().substring(5);
				temp.setThunder(Integer.valueOf(ss));

				ele = element.getElementById("du");
				temp.setSpoison(ele.text());
				ss = ele.select("p:contains(毒攻击)").text().substring(5);
				temp.setPoison(Integer.valueOf(ss));

				ele = element.getElementsByClass("fn-high-light").get(0);
				temp.setHp(Integer.valueOf(ele.text()));

				Elements es = element.getElementsByClass("box2 fn-fix-info").get(0).getElementsByClass("row2");
				temp.setEquipment(Integer.valueOf(es.get(1).select("span").text()));
				temp.setGem(Integer.valueOf(es.get(5).select("span").text()));
			}
			characters.add(temp);
		}
		new ExpToExcelService(characters).writeFile("TLBB" + System.currentTimeMillis() + ".xls");
	}
}
